package com.ogawa.joypal.distributionnetwork.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.net.wifi.ScanResult;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ogawa.joypal.distributionnetwork.R;

import java.util.List;

/**
 * 作者: bright.lin@medisana.net.cn
 * 时间: 2018/12/18
 * 标题: 自定义下拉菜单
 * 描述: 基于PopupWindow的下拉菜单
 * 修改: 由 bright.lin@medisana.net.cn 修改于 2018/12/18
 */
public class SoftApPopwindow extends PopupWindow {

    // 上下文
    private Context mContext;
    // ScanResult对象列表
    private List<ScanResult> data;
    // 列表适配器
    private SoftApPopwindowAdapter adapter;

    /**
     * 带参构造器
     *
     * @param context  上下文
     * @param data     ScanResult对象列表
     * @param listener 单个选项点击事件回调
     */
    public SoftApPopwindow(Context context, List<ScanResult> data, SoftApPopwindowAdapter.OnItemClickListener listener) {
        super(context);
        this.mContext = context;
        this.data = data;
        init(listener);
    }

    /**
     * 显示下拉菜单
     *
     * @param parent 父视图
     */
    public void showPopupWindow(View parent) {
        setWidth(parent.getWidth());
        if (!this.isShowing()) {
            Rect rect = new Rect();
            parent.getGlobalVisibleRect(rect);
            int unit = parent.getHeight();
            if (null != data) {
                if (data.size() > 5) {
                    setHeight(5 * unit);
                } else if (data.size() < 2) {
                    setHeight(unit);
                } else {
                    setHeight(data.size() * unit);
                }
            } else {
                setHeight(unit);
            }
            this.showAsDropDown(parent, 0, 0);
        } else {
            this.dismiss();
        }
    }

    public List<ScanResult> getData() {
        return data;
    }

    /**
     * 关闭下拉菜单
     */
    public void dismissPopupWindow() {
        if (this.isShowing()) {
            dismiss();
        }
    }

    /**
     * 更新数据
     *
     * @param data ScanResult对象列表
     */
    public void update(List<ScanResult> data) {
        this.data = data;
        if (null != adapter) {
            adapter.update(this.data);
        }
    }

    /**
     * 初始化
     *
     * @param listener 单个选项点击事件回调
     */
    private void init(SoftApPopwindowAdapter.OnItemClickListener listener) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_softap_popwindow, null);
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
            setFocusable(true);
            setBackgroundDrawable(null);
        } else {
            // 5.0以下设备setBackgroundDrawable不可设置为null，否则会出现无法关闭的情况
            setFocusable(false);
            setOutsideTouchable(true);
            setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        }
        RecyclerView recyclerView = view.findViewById(R.id.layout_softap_recview);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new SoftApPopwindowAdapter(data);
        adapter.setOnItemClickListener(listener);
        recyclerView.setAdapter(adapter);
    }
}
